using System;
using System.Net.Http;
using System.Threading.Tasks;
using TCEntity;

namespace TCClientService
{
  public static class AccountService
  {
    /// <summary>
    /// 判断给定用户是否合法。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task LoginAsync(this HttpClient client, User user, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Put, "./Account/login");
      await TCHttpService.SendAsync<object>(client, request, user, _ => success(), failure, InternetFailure);
    }

    /// <summary>
    /// 注册用户。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task RegisterAsync(this HttpClient client, User user, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Post, "./Account/register");
      await TCHttpService.SendAsync<object>(client, request, user, _ => success(), failure, InternetFailure);
    }
    /// <summary>
    /// 更改用户密码。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="newPassword">新密码。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task ChangePswdAsync(this HttpClient client, User user, string newPassword, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Post, "./Account/changepswd");
      await TCHttpService.SendAsync<object>(client, request, (user, newPassword), _ => success(), failure, InternetFailure);
    }
    /// <summary>
    /// 删除用户。
    /// </summary>
    /// <param name="client">HTTP客户端。</param>
    /// <param name="user">给定用户。</param>
    /// <param name="success">成功时执行的方法。</param>
    /// <param name="failure">失败时执行的方法，传入失败原因字符串。</param>
    /// <param name="InternetFailure">网络错误时执行的方法。</param>
    /// <returns></returns>
    public static async Task DeleteAccountAsync(this HttpClient client, User user, Action success, Action<string> failure, Action InternetFailure)
    {
      var request = new HttpRequestMessage(HttpMethod.Delete, "./Account/delete");
      await TCHttpService.SendAsync<object>(client, request, user, _ => success(), failure, InternetFailure);
    }

  }
}
